package com.stand.hardcode.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.stand.hardcode.util.Hash;

import com.stand.hardcode.database.PMF;

@PersistenceCapable(detachable="true")
public class StandUser implements Serializable {
	
	
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	
	@Persistent
	private String name;
	
	@Persistent
	private String email; //username aka key
	
	@Persistent
	private String description;

	@Persistent
	private ArrayList<Key> wishlist;
	
	@Persistent
	private ArrayList<Integer> wishlistQuantity;
	
	@Persistent
	private boolean isActive;
	@Persistent
	private boolean isAdmin;
	
	public StandUser(String name, String email){
		//creation of new google user
	
		this.name = name;
	
		this.email = email;
		
		this.isAdmin = false;
		this.isActive = true;
		
		this.key = KeyFactory.createKey(StandUser.class.getSimpleName(), this.email);
		
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public boolean isAdmin() {
		return isAdmin;
	}

	public void setAdmin(boolean isAdmin) {
		this.isAdmin = isAdmin;
	}

	public boolean isActive() {
		return isActive;
	}

	public void setActive(boolean isActive) {
		this.isActive = isActive;
	}

	public Key getKey() {
		return key;
	}

	public void setKey(Key key) {
		this.key = key;
	}
	
	public List<Item> getWishlist() {
		ArrayList<Item> wishlistItems = new ArrayList<Item>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		for (Key k : wishlist) {
			wishlistItems.add(pm.getObjectById(Item.class, k));
			
		}
		pm.close();
		return wishlistItems;
	}
	
	public List<Key> getWishlistKeys() {
		return wishlist;
	}
	
	public ArrayList<Integer> getWishlistQuantity() {
		return wishlistQuantity;
	}

	public void setWishlistQuantity(ArrayList<Integer> wishlistQuantity) {
		this.wishlistQuantity = wishlistQuantity;
	}

	public void addItemToWishList(long itemId, int quantity) {
		//retrieve item using the item ID
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Item item = (Item) pm.getObjectById(Item.class, itemId);
		Key itemKey = item.getKey();
		
		//add number of items to user's wishlist depending on quantity
		boolean itemInWishList = false;
		for (Key k : wishlist) {
			if (k.getId() == itemId) {
				itemInWishList = true;
			}
		}
		if (!itemInWishList) {
			wishlist.add(itemKey);
			wishlistQuantity.add(quantity);
		} else if (quantity > 0) { //prevent 0 and negatives
			int position = wishlist.indexOf(itemKey);
			if (position >= 0) {
				int currentQuantity = wishlistQuantity.get(position);
				wishlistQuantity.remove(position);
				wishlistQuantity.add(position, currentQuantity+quantity);
			}
		}
		
		pm.makePersistent(this);
		pm.close();
	}
	
	public void removeItemFromWishList(long itemId, int quantity) {
		//retrieve item using the item ID
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Item item = (Item) pm.getObjectById(Item.class, itemId);
		Key itemKey = item.getKey();
		
		//remove of items from user's wishlist depending on quantity
		int position = wishlist.indexOf(itemKey);
		int currentQuantity = wishlistQuantity.get(position);
		if (quantity >= currentQuantity) { //remove all
			wishlist.remove(itemKey);
			wishlistQuantity.remove(position);
		} else if (quantity > 0) { //prevent 0 and negatives
			int newQuantity = currentQuantity - quantity;
			wishlistQuantity.remove(position);
			wishlistQuantity.add(position, newQuantity);
		}
		
		pm.makePersistent(this);
		pm.close();
	}
}
